<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Teachersale extends Backend
{
    
    /**
     * Teachersale模型对象
     * @var \app\admin\model\Teachersale
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Teachersale;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->assignconfig("org_cate_id", ORG_CATE_ID);
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where2 = $this->getSelfDataWhere('t.org_cate_id',true);
            $total = $this->model
                    ->with(['learncourse'])
                ->join('edu_learn_cate_teacher t', 't.id = learncourse.teacher_id', 'left')
                ->join('edu_org_cate o','learncourse.org_cate_id = o.id', 'left')
                    ->where($where)
                    ->where($where2)
                ->group('t.id')
                    ->order($sort, $order)
                    ->count();
            $list = $this->model
                ->with(['learncourse'])
                ->join('edu_learn_cate_teacher t', 't.id = learncourse.teacher_id', 'left')
                ->join('edu_org_cate o','learncourse.org_cate_id = o.id', 'left')
                ->where($where)
                ->where($where2)
                ->field('SUM(CASE WHEN teachersale.price is not null THEN teachersale.price ELSE 0 END) AS total_money,SUM( CASE WHEN teachersale.type= 1 THEN teachersale.price ELSE 0 END) AS pay_money,SUM( CASE WHEN teachersale.type <> 1 THEN teachersale.price ELSE 0 END) AS left_money,t.number as tnumber,t.id as tid,t.name as tname,o.name as oname')
                ->group('t.id')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            foreach ($list as $row) {
                
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /*
 * 订单详情列表
 */

    public function sale() {
        $teacher_id = $this->request->param('ids');
        $this->view->assign("ids", $teacher_id);
        //当前是否为关联查询
        $this->relationSearch = true;
        $this->model = new \app\admin\model\order\Order;
//        $this->searchFields = 'id,name';
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//            $where2 = $this->getSelfDataWhere('order.org_cate_id');
            $total = $this->model
                ->with(['course'])
                ->join('edu_learn_cate_teacher t', 't.id = course.teacher_id', 'left')
                ->where($where)
//                ->where($where2)
                ->where(['t.id'=>$teacher_id])
                ->order($sort, $order)
                ->count();
            $total_arr = $this->model
                ->with(['course'])
                ->join('edu_learn_cate_teacher t', 't.id = course.teacher_id', 'left')
                ->where($where)
//                ->where($where2)
                ->where(['t.id'=>$teacher_id])
                ->field('SUM(CASE WHEN order.price is not null THEN order.price ELSE 0 END) AS total_money,SUM( CASE WHEN order.type= 1 THEN order.price ELSE 0 END) AS pay_money,SUM( CASE WHEN order.type <> 1 THEN order.price ELSE 0 END) AS left_money')
                ->find();
            if(empty($total_arr)){
                $total_arr['total_money'] = 0;
                $total_arr['pay_money'] = 0;
                $total_arr['left_money'] = 0;
            }
            $list = $this->model
                ->with(['course'])
                ->join('edu_learn_cate_teacher t', 't.id = course.teacher_id', 'left')
                ->where($where)
//                ->where($where2)
                ->where(['t.id'=>$teacher_id])
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list, "extend" => ['total_money' => $total_arr['total_money'], 'pay_money' =>$total_arr['pay_money'], 'left_money' =>$total_arr['left_money']]);
            return json($result);
        }
        return $this->view->fetch();
    }
}
